<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="header-info">
						<text class="tool-icon">🔍</text>
						<text class="tool-title">找物占卜</text>
						<text class="tool-subtitle">失物寻找方位指引</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">失物信息</text>
						
						<view class="form-item">
							<text class="form-label">失物名称</text>
							<input 
								v-model="params.objectName" 
								placeholder="请输入丢失物品的名称"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">物品类型</text>
							<picker 
								mode="selector" 
								:range="objectTypes" 
								:value="objectIndex"
								@change="onObjectTypeChange"
								class="type-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayObjectType }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">丢失时间</text>
							<view class="time-section">
								<picker 
									mode="date" 
									:value="params.lostDate" 
									@change="onLostDateChange"
									class="date-picker"
								>
									<view class="picker-display">
										<text class="picker-text">{{ displayLostDate }}</text>
										<text class="picker-arrow">→</text>
									</view>
								</picker>
								<picker 
									mode="time" 
									:value="params.lostTime" 
									@change="onLostTimeChange"
									class="time-picker"
								>
									<view class="picker-display">
										<text class="picker-text">{{ displayLostTime }}</text>
										<text class="picker-arrow">→</text>
									</view>
								</picker>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">丢失地点</text>
							<textarea 
								v-model="params.lostLocation" 
								placeholder="请描述丢失的具体地点或区域"
								class="location-input"
								maxlength="100"
							></textarea>
						</view>
						
						<view class="form-item">
							<text class="form-label">物品特征</text>
							<textarea 
								v-model="params.objectFeatures" 
								placeholder="请描述物品的颜色、大小、材质等特征"
								class="features-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryFindObject"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="findObjectResult">
					<view class="result-section">
						<text class="section-title">找物分析</text>
						
						<view class="find-object-analysis">
							<view class="overall-result">
								<view class="result-score">
									<view class="score-circle">
										<text class="score-text">{{ findObjectResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ findObjectResult.overall.level }}</text>
								</view>
								<view class="result-desc">
									<text class="desc-text">{{ findObjectResult.overall.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 位置分析 -->
				<app-card v-if="findObjectResult && findObjectResult.location">
					<view class="location-section">
						<text class="section-title">位置分析</text>
						
						<view class="location-analysis">
							<view class="location-grid">
								<view class="location-item area">
									<text class="location-label">区域</text>
									<text class="location-value">{{ findObjectResult.location.area }}</text>
								</view>
								<view class="location-item direction">
									<text class="location-label">方位</text>
									<text class="location-value">{{ findObjectResult.location.direction }}</text>
								</view>
								<view class="location-item height">
									<text class="location-label">高度</text>
									<text class="location-value">{{ findObjectResult.location.height }}</text>
								</view>
							</view>
							
							<view class="location-description">
								<text class="description-text">{{ findObjectResult.location.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 详细分析 -->
				<app-card v-if="findObjectResult && findObjectResult.details">
					<view class="details-section">
						<text class="section-title">详细分析</text>
						
						<view class="details-list">
							<view class="detail-item">
								<text class="detail-label">环境特征</text>
								<text class="detail-value">{{ findObjectResult.details.environment }}</text>
							</view>
							<view class="detail-item">
								<text class="detail-label">容器情况</text>
								<text class="detail-value">{{ findObjectResult.details.container }}</text>
							</view>
							<view class="detail-item">
								<text class="detail-label">遮挡情况</text>
								<text class="detail-value">{{ findObjectResult.details.situation }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 时间分析 -->
				<app-card v-if="findObjectResult && findObjectResult.timeFrame">
					<view class="time-section">
						<text class="section-title">时间分析</text>
						
						<view class="time-analysis">
							<view class="time-item best">
								<text class="time-label">最佳时机</text>
								<text class="time-value">{{ findObjectResult.timeFrame.best }}</text>
							</view>
							<view class="time-item possible">
								<text class="time-label">可能时机</text>
								<text class="time-value">{{ findObjectResult.timeFrame.possible }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 搜寻建议 -->
				<app-card v-if="findObjectResult && findObjectResult.searchTips">
					<view class="tips-section">
						<text class="section-title">搜寻技巧</text>
						
						<view class="tips-list">
							<view 
								v-for="(tip, index) in findObjectResult.searchTips" 
								:key="index"
								class="tip-item"
							>
								<view class="tip-icon">
									<text class="icon-text">🔍</text>
								</view>
								<text class="tip-text">{{ tip }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'FindObjectToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				objectName: '',
				objectType: '',
				lostDate: '',
				lostTime: '',
				lostLocation: '',
				objectFeatures: ''
			},
			findObjectResult: null,
			loading: false,
			toolsService: null,
			objectIndex: 0,
			objectTypes: [
				'首饰配件', '电子设备', '证件文件', '钥匙钱包',
				'衣服鞋帽', '书籍文具', '药品保健', '其他物品'
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.objectName.trim().length > 0 && 
				   this.params.objectType && this.params.lostDate
		},
		
		displayObjectType() {
			return this.params.objectType || '选择物品类型'
		},
		
		displayLostDate() {
			return this.params.lostDate || '选择日期'
		},
		
		displayLostTime() {
			return this.params.lostTime || '选择时间'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			
			// 设置默认时间
			const now = new Date()
			this.params.lostDate = now.toISOString().split('T')[0]
			this.params.lostTime = now.toTimeString().slice(0, 5)
			
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
		
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				uni.showToast({
					title: '已为您登录，开始占卜！',
					icon: 'none',
					duration: 2000
				})
			} else {
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		onObjectTypeChange(e) {
			this.objectIndex = e.detail.value
			this.params.objectType = this.objectTypes[e.detail.value]
		},
		
		onLostDateChange(e) {
			this.params.lostDate = e.detail.value
		},
		
		onLostTimeChange(e) {
			this.params.lostTime = e.detail.value
		},
		
		async queryFindObject() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善失物信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getFindObjectInfo(this.params)
				
				if (result.success) {
					this.findObjectResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('找物占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.type-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.time-section {
			display: flex;
			gap: 16rpx;
			
			.date-picker,
			.time-picker {
				flex: 1;
				
				.picker-display {
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: 88rpx;
					padding: 0 24rpx;
					border: 2rpx solid #f0f0f0;
					border-radius: 12rpx;
					background-color: #fafafa;
					
					.picker-text {
						font-size: 28rpx;
						color: #000000;
					}
					
					.picker-arrow {
						font-size: 24rpx;
						color: rgba(0, 0, 0, 0.6);
					}
				}
			}
		}
		
		.location-input,
		.features-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.find-object-analysis {
		.overall-result {
			display: flex;
			align-items: center;
			padding: 32rpx;
			background: linear-gradient(135deg, #795548 0%, #5D4037 100%);
			border-radius: 16rpx;
			
			.result-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.result-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
	}
}

.location-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.location-analysis {
		.location-grid {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 16rpx;
			margin-bottom: 24rpx;
			
			.location-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border-radius: 12rpx;
				
				&.area {
					background: linear-gradient(135deg, #4CAF50 0%, #388E3C 100%);
				}
				
				&.direction {
					background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
				}
				
				&.height {
					background: linear-gradient(135deg, #FF9800 0%, #F57C00 100%);
				}
				
				.location-label {
					font-size: 24rpx;
					color: rgba(255, 255, 255, 0.8);
					margin-bottom: 8rpx;
				}
				
				.location-value {
					font-size: 28rpx;
					font-weight: bold;
					color: white;
				}
			}
		}
		
		.location-description {
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.description-text {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.details-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.details-list {
		.detail-item {
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			margin-bottom: 16rpx;
			padding: 16rpx;
			background-color: #f8f9fa;
			border-radius: 8rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.detail-label {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-right: 16rpx;
				min-width: 120rpx;
			}
			
			.detail-value {
				flex: 1;
				font-size: 26rpx;
				color: #000000;
				line-height: 1.5;
			}
		}
	}
}

.time-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.time-analysis {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
		
		.time-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;
			border-radius: 12rpx;
			
			&.best {
				background-color: #e8f5e8;
				border-left: 4rpx solid #4CAF50;
			}
			
			&.possible {
				background-color: #e3f2fd;
				border-left: 4rpx solid #2196F3;
			}
			
			.time-label {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.7);
			}
			
			.time-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #000000;
			}
		}
	}
}

.tips-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.tips-list {
		.tip-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.tip-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.tip-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}
</style>